<template>
  <div class="app-container">
    <h2 style="text-align: center">发布新课程</h2>
    <el-steps
      :active="1"
      process-status="wait"
      align-center
      style="marginbottom: 40px"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="提交审核" />
    </el-steps>
    <el-form label-width="120px">
      <el-form-item label="课程标题">
        <el-input
          v-model="courseInfo.title"
          placeholder=" 示例：机器学习项目课：从基础到搭建项目视频"
        />
      </el-form-item>

      <el-form-item label="课程分类">
        <div class="block">
          <el-cascader
            v-model="value"
            :options="options"
            @change="handleChange"
            :props="{ value: 'id', label: 'title' }"
            :key="modalKey"
          ></el-cascader>
        </div>
      </el-form-item>

      <el-form-item label="课程讲师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择">
          <el-option
            v-for="teacher in teachers"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number
          :min="0"
          v-model="courseInfo.lessonNum"
          controls-position="right"
        />
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number
          :min="0"
          v-model="courseInfo.price"
          controls-position="right"
        />
      </el-form-item>

      <el-form-item label="课程封面">
        <el-upload
          class="avatar-uploader"
          :action="BASE_API + '/eduoss/fileoss'"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="courseInfo.cover" :src="courseInfo.cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="课程简介">
        <tinymce :height="300" v-model="courseInfo.description" />
      </el-form-item>

      <div>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
          >保存并下一步</el-button
        >
      </div>
    </el-form>
  </div>
</template>
<script>
import courseApi from "@/api/edu/course";
import teacherApi from "@/api/edu/teacher";
import subjectApi from "@/api/edu/subject";
import Tinymce from "@/components/Tinymce";
export default {
  components: { Tinymce },
  data() {
    return {
      saveBtnDisabled: false, // 保存按钮是否禁用
      courseInfo: {
        id:'',
        title: "",
        subjectId: "",
        subjectParentId: "",
        teacherId: "",
        lessonNum: 0,
        description: "",
        cover: "/static/default.jpg",
        price: 0,
      },
      modalKey: 1,
      BASE_API: process.env.BASE_API, // 接口API地址
      teachers: [],
      value: [],
      options: [
        {
          id: "zhinan",
          title: "指南",
          children: [
            {
              id: "shejiyuanze",
              title: "设计原则",
            },
          ],
        },
      ],
    };
  },
  created() {
    // 讲师
    this.getAllTeacher();
    // 课程分类
    this.getSubject();
    if (this.$route.params && this.$route.params.id) {
      this.getCourse(this.$route.params.id);
      this.courseInfo.id=this.$route.params.id;
    }
  },
  methods: {
    getCourse(id) {
      courseApi.getCourse(id).then((result) => {
        this.courseInfo = result.data.courseInfoVo;
        this.value[1] = result.data.courseInfoVo.subjectParentId;
        this.value[0] = result.data.courseInfoVo.subjectId;
        this.modalKey++;
        console.log(this.value);
      });
    },
    getSubject() {
      subjectApi.getNestedTreeList().then((result) => {
        this.options = result.data.items;
        console.log(this.options);
      });
    },
    handleChange(value) {
      this.courseInfo.subjectId = value[0];
      this.courseInfo.subjectParentId = value[1];
      console.log(this.value);
    },
    next() {
      if (this.courseInfo.id) {
        courseApi.updateCourse(this.courseInfo).then((result) => {
          this.$message({
            type: "success",
            message: "修改课程成功",
          });
          this.$router.push({ path: "/edu/course/chapter/" +this.courseInfo.id});
        });
      } else {
        courseApi.addCourse(this.courseInfo).then((result) => {
          console.log(result);
          this.$message({
            type: "success",
            message: "添加课程成功",
          });
          this.$router.push({ path: "/edu/course/chapter/" + result.data.id });
        });
      }
    },
    getAllTeacher() {
      teacherApi.list().then((result) => {
        this.teachers = result.data.items;
      });
    },
    handleAvatarSuccess(res, file) {
      this.courseInfo.cover = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style>
.tinymce-container {
  line-height: 29px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>